import {createRouter, createWebHistory} from 'vue-router'
import {useUserStore} from '@/stores/user'

// NProgress 配置暂时注释，避免依赖问题
// NProgress.configure({ showSpinner: false })

const routes = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/layout/index.vue'),
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/home/index.vue'),
        meta: { title: '首页' }
      },
      {
        path: '/question',
        name: 'Question',
        component: () => import('@/views/question/index.vue'),
        meta: { title: '问题' }
      },
      {
        path: '/question/:id',
        name: 'QuestionDetail',
        component: () => import('@/views/question/detail.vue'),
        meta: { title: '问题详情' }
      },

      {
        path: '/topic',
        name: 'Topic',
        component: () => import('@/views/topic/index.vue'),
        meta: { title: '话题' }
      },
      {
        path: '/topic/:id',
        name: 'TopicDetail',
        component: () => import('@/views/topic/detail.vue'),
        meta: { title: '话题详情' }
      },
      {
        path: '/spaces',
        name: 'Spaces',
        component: () => import('@/views/spaces/index.vue'),
        meta: { title: 'Space' }
      },
      {
        path: '/article',
        name: 'Article',
        component: () => import('@/views/article/index.vue'),
        meta: { title: '文章' }
      },
      {
        path: '/article/:id',
        name: 'ArticleDetail',
        component: () => import('@/views/article/detail.vue'),
        meta: { title: '文章详情' }
      },

      {
        path: '/column',
        name: 'Column',
        component: () => import('@/views/column/index.vue'),
        meta: { title: '专栏' }
      },
      {
        path: '/column/:id',
        name: 'ColumnDetail',
        component: () => import('@/views/column/detail.vue'),
        meta: { title: '专栏详情' }
      },
      {
        path: 'notifications',
        name: 'Notifications',
        component: () => import('@/views/notifications/index.vue'),
        meta: { title: 'Notifications' }
      },
      {
        path: '/search',
        name: 'Search',
        component: () => import('@/views/search/index.vue'),
        meta: { title: '搜索' }
      },
      {
        path: '/people/:id',
        name: 'Profile',
        component: () => import('@/views/people/index.vue'),
        meta: { title: '个人主页' }
      },
      {
        path: '/people/edit',
        name: 'ProfileEdit',
        component: () => import('@/views/people/edit.vue'),
        meta: { title: '编辑资料', requiresAuth: true }
      },

      {
        path: '/settings',
        name: 'Settings',
        component: () => import('@/views/settings/index.vue'),
        meta: { title: '设置', requiresAuth: true }
      },
      {
        path: '/messages',
        name: 'Messages',
        component: () => import('@/views/messages/index.vue'),
        meta: { title: '私信', requiresAuth: true }
      },
      {
        path: '/answers',
        name: 'Answers',
        component: () => import('@/views/answers/index.vue'),
        meta: { title: 'Answers' }
      },
      {
        path: '/following',
        name: 'Following',
        component: () => import('@/views/following/index.vue'),
        meta: { title: 'Following' }
      }
    ]
  },

  {
    path: '/ask',
    name: 'Ask',
    component: () => import('@/views/ask/index.vue'),
    meta: { title: '提问', requiresAuth: true }
  },
  {
    path: '/write',
    name: 'Write',
    component: () => import('@/views/write/index.vue'),
    meta: { title: '写文章', requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/auth/login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/auth/register.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: { title: '页面不存在' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  // NProgress.start()
  
  const userStore = useUserStore()
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - quora`
  }
  
  // 检查是否需要登录
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
    return
  }
  
  // 已登录用户访问登录/注册页面，重定向到首页
  if ((to.name === 'Login' || to.name === 'Register') && userStore.isLoggedIn) {
    next('/')
    return
  }
  
  next()
})

router.afterEach(() => {
  // NProgress.done()
})

export default router
